﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Text.RegularExpressions;

namespace BusinessClass
{
    /// <summary>
    /// Interaction logic for ConfigSetting.xaml
    /// </summary>
    public partial class ConfigSetting : Window
    {
        public ConfigSetting()
        {
            InitializeComponent();
        }
        #region 生成图片配置
        List<ImgConfig> imgConfigs = new List<ImgConfig>();
        private void btnMakeImgConfigReMake_Click(object sender, RoutedEventArgs e)
        {
            imgConfigs.Clear();
            makeImgsConfigs(0);
            showImgConfig();
        }
        private void makeImgsConfigs(int baseIndex)
        {
            txtImgConfigs.Text = "";
            double w = Convert.ToDouble(txtImgConfigW.Text);
            double h = Convert.ToDouble(txtImgConfigH.Text);
            var name = txtImgName.Text;
            var i = name.IndexOf('{');
            var len = Convert.ToInt32(name.Substring(i + 1, 1));
            string left = name.Substring(0, i);
            string right = name.Substring(i + 3);
            int count = Convert.ToInt32((cbImgConfigMakeCount.SelectedItem as ComboBoxItem).Content);
            for (int j = baseIndex; j < baseIndex + count; j++)
            {
                imgConfigs.Add(new ImgConfig() { Name = left + LeftFillZero(j, len) + right, H = h, W = w });
            }
        }
        private void showImgConfig()
        {
            foreach (var item in imgConfigs)
            {
                txtImgConfigs.Text += item.ToString();
            }
        }
        private string LeftFillZero(int v, int len)
        {
            StringBuilder sb = new StringBuilder();
            var vv = v.ToString();
            if (vv.Length < len)
            {
                for (int i = 0; i < len - vv.Length; i++)
                {
                    sb.Append("0");
                }
            }
            return sb.ToString() + vv;
        }

        private void btnMakeImgConfigAppend_Click(object sender, RoutedEventArgs e)
        {
            makeImgsConfigs(imgConfigs.Count);
            showImgConfig();
        }
        #endregion

        private void cbHV_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (cbHV.SelectedIndex == 1)//垂直
            {

            }
        }
    }
    public class ImgConfig
    {
        public string Name { get; set; }
        public double W { get; set; }
        public double H { get; set; }
        public override string ToString()
        {
            return string.Format("名称：{0}宽{1}高{2}\r\n", Name, W.ToString(), H.ToString());
        }
        public static ImgConfig FromString(string str)
        {
            var ptn = "名称：(.+?)宽{.+?}高{.+?}";
            var m = Regex.Match(str, ptn);
            return new ImgConfig()
            {
                Name = m.Captures[1].Value,
                W = Convert.ToDouble(m.Captures[2].Value),
                H = Convert.ToDouble(m.Captures[3])
            };
        }
        public string ToJson()
        {
            return string.Format("{ \"name\":\"{0}\",\"w\":{1},\"h\":{2}}", Name, W, H);
        }
    }
}
